// student_manager.c
#include <stdbool.h> 

#include <stdio.h>
#include <string.h>
#include "../include/link_pub.h"
#include "../include/course_manager.h"

#define MAX_STUDENTS 100
#define MAX_COURSES 10
#define STUDENT_FILE "/home/chp/桌面/MyProject/txt_File/student.txt"


extern Course courses[MAX_COURSES]; // 声明为外部变量
extern int course_count;           // 声明为外部变量

typedef struct {
    char student_id[20];
    char name[50];
    char enrolled_courses[MAX_COURSES][20]; // 已选课程列表
    int total_credits; // 已选课程总计学分
    int num_enrolled_courses; // 已选课程数量
} Student;

Student students[MAX_STUDENTS];
int student_count = 0;

// 从文件加载学生信息
void load_students() {
    FILE *file = fopen(STUDENT_FILE, "r");
    if (file != NULL) {
        while (fscanf(file, "%s %s %d", students[student_count].student_id, students[student_count].name, &students[student_count].total_credits) != EOF) {
            students[student_count].num_enrolled_courses = 0; // 初始化已选课程数量
            student_count++;
        }
        fclose(file);
    }
}

// 保存学生信息到文件
void save_students() {
    FILE *file = fopen(STUDENT_FILE, "w");
    for (int i = 0; i < student_count; i++) {
        fprintf(file, "%s %s %d\n", students[i].student_id, students[i].name, students[i].total_credits);
    }
    fclose(file);
}

// 新增学生
void add_student(const char* student_id, const char* name) {
    if (student_count < MAX_STUDENTS) {
        strcpy(students[student_count].student_id, student_id);
        strcpy(students[student_count].name, name);
        students[student_count].total_credits = 0;
        students[student_count].num_enrolled_courses = 0;
        student_count++;
        save_students();
        log_operation("add_student", student_id);
    }
}

// 删除学生
void delete_student(const char* student_id) {
    for (int i = 0; i < student_count; i++) {
        if (strcmp(students[i].student_id, student_id) == 0) {
            // 减少学生所选课程的选课人数
            for (int j = 0; j < students[i].num_enrolled_courses; j++) {
                decrease_enrolled_students(students[i].enrolled_courses[j]);
            }
            // 删除学生
            for (int j = i; j < student_count - 1; j++) {
                students[j] = students[j + 1];
            }
            student_count--;
            save_students();
            log_operation("delete_student", student_id);
            break;
        }
    }
}

// 更新学生信息
void update_student(const char* student_id, const char* new_name) {
    for (int i = 0; i < student_count; i++) {
        if (strcmp(students[i].student_id, student_id) == 0) {
            strcpy(students[i].name, new_name);
            save_students();
            log_operation("update_student", student_id);
            break;
        }
    }
}

// 列出所有学生ID和姓名
void list_students() {
    printf("学生ID\t姓名\n");
    for (int i = 0; i < student_count; i++) {
        printf("%s\t%s\n", students[i].student_id, students[i].name);
    }
}

// 查看学生信息
void view_student(const char* student_id) {
    for (int i = 0; i < student_count; i++) {
        if (strcmp(students[i].student_id, student_id) == 0) {
            printf("学生ID: %s\n姓名: %s\n总学分: %d\n已选课程:\n", students[i].student_id, students[i].name, students[i].total_credits);
            for (int j = 0; j < students[i].num_enrolled_courses; j++) {
                for (int k = 0; k < course_count; k++) {
                    if (strcmp(students[i].enrolled_courses[j], courses[k].course_id) == 0) {
                        printf("  课程ID: %s\t课程名称: %s\n", courses[k].course_id, courses[k].name);
                        break;
                    }
                }
            }
            printf("\n");
            return;
        }
    }
    printf("未找到学生！\n");
}

// 学生选课功能
void select_course(const char* student_id, const char* course_id) {
    // 首先检查课程是否已满
    for (int i = 0; i < course_count; i++) {
        if (strcmp(courses[i].course_id, course_id) == 0) {
            if (courses[i].enrolled_students < courses[i].max_students) {
                // 课程未满，继续选课操作
                for (int j = 0; j < student_count; j++) {
                    if (strcmp(students[j].student_id, student_id) == 0) {
                        if (students[j].num_enrolled_courses < MAX_COURSES) {
                            strcpy(students[j].enrolled_courses[students[j].num_enrolled_courses], course_id);
                            students[j].total_credits += 4; // 假设每门课程4学分
                            students[j].num_enrolled_courses++;
                            courses[i].enrolled_students++; // 更新课程的选课人数
                            save_students(); // 保存学生信息
                            save_courses(); // 保存课程信息
                            log_operation("select_course", student_id);
                            log_operation("enrolled_course", course_id);
                            printf("选课成功！\n");
                        } else {
                            printf("学生已达到最大选课数量！\n");
                        }
                        return;
                    }
                }
                printf("未找到学生！\n");
                return;
            } else {
                // 课程已满，打印消息并返回
                printf("课程已满，无法选课: %s\n", course_id);
                return;
            }
        }
    }
    // 未找到课，打印消息
    printf("未找到课程: %s\n", course_id);
}

